<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

abstract class MY_Controller extends CI_Controller
{
	/**
     * _admin
     * 保存当前登录用户的信息
     *
     * @var object
     * @access  public
     **/
	public $attachment_size;
	public $attachment_type;
	public $attachment_type_str;
	
	/**
     * 构造函数
     *
     * @access  public
     * @return  void
     */
	function __construct()
	{
		parent::__construct();
		$this->_check_login();
		$this->load->library('acl');
		
		//提取允许的附件类型
		$query = $this->db->select('attachment_type,attachment_maxupload')->get('site_settings');
		$row = $query->row(); 
		$this->attachment_size = $row->attachment_maxupload.'MB';
		$this->attachment_type = explode(",",$row->attachment_type);
		if($this->attachment_type == ''){
			$this->attachment_type_str = '*.*';
		}else{
			foreach($this->attachment_type as $value){
				$this->attachment_type_str .= '*.'.$value.'; ';
			}
		}
	}
	
	//JSON成功或错误方法
	function rejsonsuccess($b=false,$str="未知错误"){
		if(!$b){
			echo json_encode(array("success"=>false,"errorMsg"=>$str));
		}else{
			echo json_encode(array("success"=>true)); 
		}	
	}
	// ------------------------------------------------------------------------

    /**
     * 检查用户是否登录
     *
     * @access  protected
     * @return  void
     */
	protected function _check_login()
	{
		$this->load->library('user_agent');
		if (!stristr($this->agent->agent_string(),'shockwave')){//如果是flash提交的则不检查是否登录
			if ( ! $this->session->userdata('user_name'))
			{   
				redirect('login');
				exit();
			}
		}
	}
	
		//附件列表的公开方法
	/* 四个参数分别为：模块名，ID字符串，文件名字符串
	*/
	function attlist($m,$ids,$names,$sizes,$id='',$ps_id=''){
		if($ids != '' && $names != ''){
			$att_ids = explode(',',reduce_multiples($ids,',',true));
			$att_names = explode('*',reduce_multiples($names,'*',true));  //文件名分隔符是*号
			
			$att_sizes = explode(',',reduce_multiples($sizes,',',true));
			//$att_class = dechex(rand(00000000,99999999));
			
			//附件列表面板ID为panel_attlist_加模块名加ID号
			$str = "<div id='panel_attlist_".$m.$id."' class='easyui-panel' title='共（".count($att_names)."个）附件' style='padding:5px;width:100%;'>";

			$min_att = (count($att_ids)>count($att_names))?count($att_names):count($att_ids);

			for($i = 0; $i<$min_att;$i++){
				$attsuffix = strtolower(end(explode('.',$att_names[$i])));	//文件名后缀转小写
				//$date_rand = explode('_',$att_ids[$i]);	//拆解日期和随机数字符串
				//$filename = FCPATH.'attach\\'.$m.'\\'.reset($date_rand).'\\'.end($date_rand).'.'.$att_names[$i];//拼接字符串
				//$filename = iconv('UTF-8', 'GB2312', $filename);
				//$v_size = @filesize($filename);	//提取文件大小
				//$v_size = (!$v_size)?'':' （'.$this->size2mb($v_size).'）';//如果去到了文件大小才显示括号和转换后的值
				$v_size = (empty($att_sizes[$i]))?'':' （'.byte_format($att_sizes[$i],2).'）';//如果去到了文件大小才显示括号和转换后的值
				$i_size = (empty($att_sizes[$i]))?'0':$att_sizes[$i];//如果出现未定义的偏移量，则以0字节填充
				$str .= "<div class='eattl' style='margin-top:5px;'>";
				$str .= "<input id='att_m' type='hidden' value='".$m."'>";
				$str .= "<input id='att_id' type='hidden' value='".$att_ids[$i]."'>";
				$str .= "<input id='att_name' type='hidden' value='".$att_names[$i]."'>";
				$str .= "<input id='att_size' type='hidden' value='".$i_size."'>";
				if($id != ''){
					$str .= "<input id='run_id' type='hidden' value='".$id."'>";
					$str .= "<input id='ps_id' type='hidden' value='".$ps_id."'>";
				}
				$str .= "<a href='javascript:void(0)' onclick=\"att_down('".$m."','".$att_ids[$i]."','".$att_names[$i]."')\" class='att_btn' style='text-align:left;'><div class='icon-filetype-".strtolower($attsuffix)."' style='width:16px; height:16px; float:left; margin:3px 5px 0 0;'></div>".$att_names[$i].$v_size."</a>";
				$str .= "</div>";
			}
			$str .= "</div>";
			clearstatcache();//清除读取的文件缓存
			return $str;
		}
	}
}
	